package com.dw.etl.core.service;

import com.dw.etl.common.base.BaseException;
import com.dw.etl.common.vo.FieldAttrVO;
import com.dw.etl.common.vo.PaginationVO;
import com.dw.etl.core.api.*;
import com.dw.etl.core.bean.model.BaseDb;
import com.dw.etl.core.bean.model.BaseDbTpcomp;
import com.dw.etl.core.bean.model.EtlBase;
import com.dw.etl.core.bean.model.EtlBaseField;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by liujunlei on 2021/6/2.
 */
@Service
public class EtlRunServiceImpl implements EtlRunService {

    @Autowired
    BaseDbService baseDbService;
    @Autowired
    BaseDbTpcompService baseDbTpcompService;
    @Autowired
    EtlBaseFieldService etlBaseFieldService;
    @Autowired
    EtlBaseService etlBaseService;
    @Autowired
    TableOperService tableOperService;

    @Override
    public void autoCreatTable(Long idBase) {
        EtlBase etlBase = etlBaseService.getById(idBase);
        String tableName = etlBase.getTbTar();
        EtlBaseField etlBaseField = new EtlBaseField();
        etlBaseField.setIdBase(idBase);
        List<EtlBaseField> etlBaseFieldList = etlBaseFieldService.findByModel(etlBaseField);
        BaseDb souDb = baseDbService.getById(etlBase.getIdDbSou());
        BaseDbTpcomp dbTpcomp = new BaseDbTpcomp();
        dbTpcomp.setEuDbtp(souDb.getEuTp());
        List<BaseDbTpcomp> dbTpcompList = baseDbTpcompService.findByModel(dbTpcomp);
        List<FieldAttrVO> list = tableOperService.findFieldMsgFromDb(souDb,tableName);
        if(list.size()>0) { // 存在执行更新

        } else {  //自动建表
            StringBuffer creatSql = new StringBuffer("create table "+tableName+"(");
            Map<String,BaseDbTpcomp> dbTpcompMap = new HashMap<>();
            boolean haveField = false;
            for(EtlBaseField field:etlBaseFieldList) {
                for(BaseDbTpcomp comp:dbTpcompList) {
                    if(field.getEuType().equals(comp.getEuJavatp())) {
                        if(field.getNumPrec()>0 && comp.getFgDeci()>0) { //小数
                            if(field.getNumLength()<comp.getNumLength()) {
                                creatSql.append(field.getField()+" "+comp.getEuFldtp()+"("+field.getNumLength()+","+field.getNumPrec()+"),");
                                haveField = true;
                                break;
                            }
                        } else if(field.getNumPrec()==0 && comp.getFgDeci()==0) {
                            if(field.getNumLength()<comp.getNumLength()) {
                                creatSql.append(field.getField()+" "+comp.getEuFldtp()+"("+field.getNumLength()+"),");
                                haveField = true;
                                break;
                            }
                        }
                    }
                }
            }
            if(haveField) { //可以创建
                creatSql = creatSql.deleteCharAt(creatSql.length()-1);
                creatSql.append(")");
                tableOperService.execDdlSqlByDb(creatSql.toString(),etlBase.getIdDbTar());
                //执行创建
            }
        }
    }
}
